/**
 * @class DBActions
 * @description Class to communicate with the database with ADD & GET functionality
 */
var DBActions = function()
{
	var DB;

	/**
	 * Init the DB Connection from the LocalDB Class
	 */
	this.initDBConnection = function()
	{
		DB = localDB.getDBConnection();
	}


	/*****************************************************************
	 * ADD [INSERT]
	 ****************************************************************/


	/*
	 * Add a new user
	 * @param user
	 */
	this.addUser = function(user)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO users (userId, username, age, image, gender, playcount, registerdate) VALUES (?, ?, ?, ?, ?, ?, ?)";
				t.executeSql(query, [user.id, user.name, user.age, user.image, user.gender, user.playcount, user.registeredDate]);
			}
		);
	}

	/*
	 * Add an Artist
	 *
	 * @param artist Artist
	 */
	this.addArtist = function(artist)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO artists (artistId, name, image, url) VALUES (?, ?, ?, ?)";
				t.executeSql(query, [artist.id, artist.name, artist.image, artist.url]);
			}
		);
	}

	/*
	 * Add an User and an Artist to combine them by an Unique value
	 *
	 * @param userId string
	 * @param artistId string
	 */
	this.addArtistByUser = function(userId, artistId)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO userArtists (userId, artistId) VALUES (?, ?)";
				t.executeSql(query, [userId, artistId]);
			}
		);
	}

	/**
	 * Add an album that belongs to an artist
	 *
	 * @param artistId string
	 * @param album Album
	 */
	this.addAlbumByArtistId = function(artistId, album)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO albums (albumId, artistId, name, image, url) VALUES (?, ?, ?, ?, ?)";
				t.executeSql(query, [album.id, artistId, album.name, album.image, album.url]);
			}
		);
	}

	/**
	 * Add an friend to an User
	 *
	 * @param username string
	 * @param friendUsername string
	 */
	this.addUserFriend = function(username, friendUsername)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO friends (username, friendUsername) VALUES (?, ?)";
				t.executeSql(query, [username, friendUsername]);
			}
		);
	}

	/**
	 * Add the compiron score between an User and a friend
	 *
	 * @param username string
	 * @param friendUsername string
	 * @param score float
	 * @param artistArray Array
	 */
	this.addUserComparions = function(username, friendUsername, score, artistArray)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "INSERT INTO comparisons (username, friendUsername, value) VALUES (?, ?, ?)";
				t.executeSql(query, [username, friendUsername, score], function(t, r)
				{
					for (var i = 0; i < artistArray.length; i++)
					{
						var query = "INSERT INTO comparisonArtists (comparisonId, artistName) VALUES (?, ?)";
						t.executeSql(query, [r.insertId, artistArray[i].name]);
					}
				});
			}
		);
	}


	/*****************************************************************
	 * GET [SELECT ROWS]
	 ****************************************************************/


	/**
	 * Get a user by his username
	 *
	 * @param username string
	 * @param friendsCount int
	 * @param artistsCount int
	 */
	this.getUserByName = function(username, friendsCount, artistsCount)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "SELECT * FROM users WHERE username = (?)";
				t.executeSql(query, [username], function(t, r)
				{
					if (r.rows.length > 0)
					{
						//Prepare DB result for object Actions
						var dbUser = r.rows.item(0);
						dbUser["id"] = dbUser["userId"];
						dbUser["name"] = dbUser["username"];
						dbUser["registered"] = dbUser["registerdate"];

						//Create & Initalise new User
						var user = new User();
						user.init(dbUser);
						initNewUser(user);

						//Add friends & artist to Object
						user.getFriends(friendsCount);
						user.getArtists(artistsCount);
					}
				});
			}
		);
	}

	/**
	 * Get friends by a user
	 *
	 * @param user User
	 */
	this.getFriendsByUser = function(user)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "SELECT * FROM friends WHERE username = (?)";
				t.executeSql(query, [user.name], function(t, r)
				{
					if (r.rows.length > 0)
					{
						//Hide the loader & show the data after friend have been loaded
						toggleWrapperLoader();

						//Loop through result friends
						for (var i = 0; i < r.rows.length; i++)
						{
							//Prepare DB result for object Actions
							var dbFriend = r.rows.item(i);
							dbFriend["name"] = dbFriend["friendUsername"];

							//Push friend within userObject & show within the box
							user.friends.push(dbFriend);
							showFriendInBox(dbFriend);
						}
					}
				});
			}
		);
	}

	/**
	 * Get Artists by User
	 *
	 * @param user User
	 */
	this.getArtistsByUser = function(user)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "SELECT * FROM artists AS ar "+
							"JOIN userArtists AS ua ON ar.artistId = ua.artistId "+
							"WHERE ua.userId = (?)";
				t.executeSql(query, [user.id], function(t, r)
				{
					if (r.rows.length > 0)
					{
						//Loop through result friends
						for (var i = 0; i < r.rows.length; i++)
						{
							//Prepare DB result for object Actions
							var dbArtist = r.rows.item(i);
							dbArtist["id"] = dbArtist["artistId"];

							//Create new artist & getAlbums
							var newArtist = new Artist();
							newArtist.init(dbArtist);
							newArtist.getAlbums();

							//Push artist into UserObject
							user.artists.push(newArtist);
						}
					}
				});
			}
		);
	}

	/**
	 * Get topAlbums from the given artist
	 *
	 * @param artist Artist
	 */
	this.getAlbumsByArtist = function(artist)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "SELECT * FROM albums WHERE artistId = (?)";
				t.executeSql(query, [artist.id], function(t, r)
				{
					if (r.rows.length > 0)
					{
						//Loop through result friends
						for (var i = 0; i < r.rows.length; i++)
						{
							//Prepare DB result for object Actions
							var dbAlbum = r.rows.item(i);
							dbAlbum["id"] = dbAlbum["albumId"];

							//Create new Album
							var album = new Album();
							album.init(dbAlbum);

							//Push album into ArtistObject
							artist.albums.push(album);
						}
					}
				});
			}
		);
	}

	/**
	 * Get the Compare taste of the two given users
	 *
	 * @param user1 string
	 * @param user2 string
	 */
	this.getUserComparison = function(user1, user2)
	{
		DB.transaction
		(
			function(t)
			{
				var query = "SELECT * "+
							"FROM comparisons "+
							"WHERE username = (?) AND friendUsername = (?)";
				t.executeSql(query, [user1, user2], function(t, r)
				{
					if (r.rows.length > 0)
					{
						//Prepare DB result for object Actions
						var dbCompare = r.rows.item(0);

						//Get the score from the DB row
						var score = dbCompare.value;

						//Start a new query to get the comparison Artists
						var query = "SELECT ca.*, ar.name, ar.image, ar.url, ar.artistId AS mbid "+
									"FROM comparisonArtists AS ca "+
									"LEFT JOIN artists AS ar ON ca.artistName = ar.name "+
									"WHERE ca.comparisonId = (?)";
						t.executeSql(query, [dbCompare.comparisonId], function(to, ro)
						{
							//The Array to put the artists in
							var artistArray = Array();
							
							for (var i = 0; i < ro.rows.length; i++)
							{
								var dbArtist = ro.rows.item(i);

								//If the Artist is not in the DB, we won't push it..
								if (dbArtist.mbid != null)
								{
									var artist = new Artist();
									artist.init(dbArtist);
									artist.image = dbArtist.image;
									artistArray.push(artist);
								}
							}

							//Handle compare data
							handleUserCompareData(artistArray, user2, score);
						});
					}
				});
			}
		);
	}
}